'use strict';

angular.module('myApp.students', ['tm.pagination'])
.controller('studentsCtrl', function($scope,$http,$location){

	$('#search_input').hide();

	//下拉菜单：筛选类型
	$scope.selectTypes = [
		{id : '1', name : '所在班级'},
		{id : '2',name : '姓名'},
		{id : '3',name : '学号'}
	];
	$scope.selectType = $scope.selectTypes[0].id;

	//下拉菜单：显示所有班级
	$http.get('v1/classes').success(function(data){
		$scope.values = [];
        var data = data.resule.data
        for (var i = 0; i < data.length; i++) {
            $scope.values.push({'id':data[i].id,"name":data[i].grade+data[i].class});
        };
        $scope.selectClass = $scope.values[0].id;

        //筛选类型发生变化时
		var column = 'classes_id';
		var content = $scope.selectClass;
		$scope.update = function() {
			if($scope.selectType == "1"){
				column="classes_id";
				content = $scope.selectClass;
				$('#search_select2').show();
				$('#search_input').hide();
			}else{
				$('#search_select2').hide();
				$('#search_input').show();
				if ($scope.selectType == "2") {
					column = "name";
				}else{
					column = "stuid";
				}

			};
		}

		$scope.update2 = function() {
			content = $scope.selectClass;
		}
		$scope.update3 = function() {
			content = $scope.content;
		}
		//搜索
		$("#search_send_button").on('click',function(){
	 		$http({
			    url: 'v1/student', 
			    method: "GET",
			    params: {column: column,content:content}
			 }).success(function(data){
			 	console.log(data);
	            if(!data.error ){
	 			$scope.students = data.result['data'];           	
	            }else{
	                alert(data.message);
	            }
	 		});
		});
	});

	//默认显示所有学生
	$http.get('v1/student').success(function(data){
		var temp_result = data.result['data'];
		var temp_result_length = temp_result.length;
		for( var i=0; i<temp_result_length ; i++ ){
			if( temp_result[i].sex == 1 )
				temp_result[i].sex = '男';
			else
				temp_result[i].sex = '女';
			switch(temp_result[i].relation){
				case 1 :temp_result[i].relation = "父亲";break;
				case 2 :temp_result[i].relation = "母亲";break;
				case 3 :temp_result[i].relation = "爷爷";break;
				case 4 :temp_result[i].relation = "奶奶";break;
				case 5 :temp_result[i].relation = "其他";break;
				default : break;
			}
		}
		$scope.students= temp_result;
	});

	
	//显示全部
	$scope.reload = function(){
		$http.get('v1/student').success(function(data){
			var temp_result = data.result['data'];
			var temp_result_length = temp_result.length;
			for( var i=0; i<temp_result_length ; i++ ){
				if( temp_result[i].sex == 1 )
					temp_result[i].sex = '男';
				else
					temp_result[i].sex = '女';
			}
			$scope.students= temp_result;
		});		
	};
	
	//分页
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 8000,
        itemsPerPage: 15,
        pagesLength: 15,
        perPageOptions: [10, 20, 30, 40, 50],
        rememberPerPage: 'perPageItems',
        onChange: function(){

        }
    };


	//跳转到新建
    $scope.jumpToUrl = function(path) {
        $location.path(path);
        var curUrl = $location.absUrl();
    };

	//删除学生
	$scope.delete = function(student){
		$http.delete('v1/student/'+student.id).success(function(data){
			if( !data.error ){
				$scope.students.splice($scope.students.indexOf(student),1);
				alert("删除成功","success");
			}else{
				alert(data.message);
			}
		});
	};
	//编辑学生信息
	// $scope.edit = function(){
	// 	$http.put('v1/student/'+student.id,column:id,content:student.id).success(function(data){
	// 		if( !data.error ){
				
	// 		}else{
	// 			alert(data.message);
	// 		}
	// 	});
	// };
	
});

